<template>
    <transition name="fade">
        <div class="media-wrapper">
            <el-button type="primary" class="media-close" icon="el-icon-close" @click="close"></el-button>
            <div class="date-picker">
                <span>设置查询起止时间：</span>
                <Select v-model="st" style="width:150px">
                    <Option v-for="item in timeList" :value="item" :key="item">{{ item }}</Option>
                </Select>
                <span>~</span>
                <Select v-model="et" style="width:150px">
                    <Option v-for="item in timeList" :value="item" :key="item">{{ item }}</Option>
                </Select>
                <el-radio-group class="speed-ctrl" v-model="speed" style="color:#fff">
                    <el-radio label="慢"></el-radio>
                    <el-radio label="中"></el-radio>
                    <el-radio label="快"></el-radio>
                </el-radio-group>
            </div>
            <el-progress class="progress-bar" :percentage="progress" :stroke-width=6 :hide-info=true status="active"></el-progress>
            <div class="media-controller">
                <el-button-group shape='circle'>
                    <el-button size="large" type="primary" icon="fa fa-backward" @click.prevent="prev"></el-button>
                    <el-button size="large" v-if="isPlay" type="primary" icon="fa fa-play" @click.prevent="start"></el-button>
                    <el-button size="large" v-else type="primary" icon="fa fa-pause" @click.prevent="start"></el-button>
                    <el-button size="large" v-show="nowIndex!=0" type="primary" icon="fa fa-stop" @click.prevent="stop"></el-button>
                    <el-button size="large" type="primary" icon="fa fa-forward" @click.prevent="next"></el-button>
                </el-button-group>
            </div>
            <div class="media-content">
                <!-- <div v-for="(item,index) in seeMedia" :key="index" :class="type(index)">
                                    <img :src='item' @click="toggle(index)">
                                    <!-- <video :src="item" v-else controls="controls" @click="toggle(index)">
                                    </video> -->
                <!-- </div> -->
                <div v-for="(item,index) in seeMedia" :key="index" :class="type(index)">
                    <img :src='item' @click="toggle(index)">
                    <!-- <video :src="item" v-else controls="controls" @click="toggle(index)">
                                    </video> -->
                </div>
            </div>
        </div>
    </transition>
</template>

<script>
import { satelliteImgFormat, timeList } from "../../scripts/weather.js"

export default {
    name: 'cjMedia',
    data: function() {
        return {
            nowIndex: 0,
            st: "",
            et: "",
            speed: "中",
            isPlay: true,
            interval: null
        }
    },
    methods: {
        next() {
            if (this.nowIndex == this.seeMedia.length - 1) {
                // this.$Message.warning('已到达最后一张');
                this.nowIndex = 0;
            } else {
                this.nowIndex++;
            }
        },
        prev() {
            if (this.nowIndex == 0) {
                // this.$Message.warning('已到达第一张');
                this.nowIndex = this.seeMedia.length - 1;
            } else {
                this.nowIndex--;
            }
        },
        type(index) {
            if (index == this.nowIndex) {
                return 'media-center'
            } else if (index - this.nowIndex == 1) {
                return 'media-right'
            } else if (index - this.nowIndex == -1) {
                return 'media-left'
            } else {
                return 'media-hide'
            }
        },
        isImg(item) {
            var ext = item.substr(item.length - 3, 3);
            var flag = ext == ('jpg' || 'png' || 'gif') ? true : false;
            return flag;
        },
        toggle(index) {
            if (index - this.nowIndex == 1) {
                this.nowIndex++;
            } else if (index - this.nowIndex == -1) {
                this.nowIndex--;
            } else {
                this.isFull = true;
            }
        },
        close() {
            this.$store.commit("TOGGLE_WEATHER_SHOW", false);
            if(this.interval) clearInterval(this.interval);
        },
        start() {
            let that = this, second;
            if (this.speed == "慢") {
                second = 1500
            } else if (this.speed == "中") {
                second = 1000
            } else {
                second = 500
            }
            this.isPlay = !this.isPlay;
            this.interval = setInterval(function() {
                if(that.isPlay){
                    clearInterval(that.interval);
                }else{
                    that.nowIndex++;
                }
                if (that.nowIndex == that.seeMedia.length - 1) {
                    // that.$Notice.info({ title: "播放完毕" });
                    // clearInterval(interval);
                    that.nowIndex = 0;
                    // if (this.isPlay) {
                    //     if (interval) clearInterval(interval);
                    // }
                    
                }
            }, second)
        },
        stop() {
            if(this.interval) clearInterval(this.interval);
            this.nowIndex = 0;
        }
    },
    computed: {
        weatherType() {
            return this.$store.state.weatherType;
        },
        seeMedia() {
            return satelliteImgFormat(this.st, this.et, this.weatherType);
        },
        timeList() {
            return timeList(this.weatherType).reverse();
        },
        progress() {
            return (this.nowIndex / (this.seeMedia.length - 1)) * 100;
        }
    },
    mounted() {
        this.st = this.timeList[0];
        this.et = this.timeList[this.timeList.length - 1];
    }
}
</script>

<style lang="scss">
.media-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 980px;
    background: rgba(0, 0, 0, 0.62);
    z-index: 1010;
    .progress-bar {
        position: absolute;
        bottom: 70px;
        width: calc(100% - 20px);
        margin: 0 10px;
    }
    .date-picker {
        position: absolute;
        bottom: 20px;
        left: 20px;
        z-index: 1012;
        &>span {
            font-size: 16px;
            color: #fff;
        }
        .ivu-select-dropdown {
            transform: translateY(calc(-100% - 42px));
        }
    }
    .speed-ctrl {
        display: inline-block;
        color: #fff;
        margin-left: 30px;
    }
    i {
        color: #fff
    }
    .media-controller {
        position: absolute;
        left: 50%;
        bottom: 20px;
        transform: translateX(-50%)
    }
    .media-close {
        position: absolute;
        right: 5px;
        top: 5px;
        i {
            font-size: 30px;
        }
    }
    .media-content {
        div {
            position: absolute;
            top: 38%; // background: green;
            color: #fff;
            text-align: center;
            font-size: 30px; // transition: all .4s ease;
            cursor: pointer;
            img {
                // max-width: 100%;
                // max-height: 100%
                width: 100%
            }
            video {
                width: 100%;
            }
        }
        .media-center {
            height: 70%;
            width: 60%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 1011;
        }
        .media-left,
        .media-right {
            display: none;
            width: 25%;
            height: 35%;
            filter: grayscale(90%);
        }
        .media-right {
            left: 100%;
            transform: translate(-110%, -50%) perspective(800px) rotateY(-20deg);
        }
        .media-left {
            left: 0;
            transform: translate(10%, -50%) perspective(800px) rotateY(20deg);
        }
        .media-hide {
            height: 0;
            width: 0;
            left: 50%;
            z-index: 1010;
            opacity: 0;
        }
    }
    .el-radio__label{
        color: #fff;
    }
}
</style>